import { apiWrapper } from '@/utils/request'

// ========================= 管理员存款类型管理API (/Admin/deposit) =========================
export const adminDepositApi = {
  // 新增存款类型
  addDeposit: (depositData) => {
    return apiWrapper.post('/Admin/deposit/addDeposit', depositData)
  },

  // 更新存款类型
  updateDeposit: (depositData) => {
    return apiWrapper.put('/Admin/deposit/updateDeposit', depositData)
  },

  // 删除存款类型
  deleteDeposit: (id) => {
    return apiWrapper.delete(`/Admin/deposit/deleteDeposit/${id}`)
  },

  // 获取存款类型信息
  getDeposit: (savingid) => {
    return apiWrapper.get(`/Admin/deposit/getDeposit/${savingid}`)
  },

  // 分页查询存款类型
  pageDeposit: (queryData, begin, end) => {
    // 构建正确的请求体，匹配DepositQueryDTO，避免null值导致后端NPE
    const requestBody = {
      savingname: queryData.savingname || '',
      page: queryData.page || 1,
      pageSize: queryData.pageSize || 20
    }

    // 构建查询参数
    const params = new URLSearchParams()
    if (begin) params.append('begin', begin)
    if (end) params.append('end', end)

    const url = `/Admin/deposit/pageDeposit${params.toString() ? '?' + params.toString() : ''}`
    
    return apiWrapper.post(url, requestBody)
  },
}

// 兼容旧版本的导出
export const getDeposits = adminDepositApi.pageDeposit
export const addDeposit = adminDepositApi.addDeposit
export const updateDeposit = adminDepositApi.updateDeposit
export const deleteDeposit = adminDepositApi.deleteDeposit
export const getDeposit = adminDepositApi.getDeposit 